package com.example.jacky.android_easeui.netapi;

import android.text.TextUtils;

import java.io.Serializable;

public class ResponseDto<Payload> implements Serializable {

    private static final long serialVersionUID = -8926494529609252631L;

    public static final String SUCCESS_CODE = "SUCCESS";

    public static final String FAIL_CODE = "FAIL";

    public static final String OUT_OF_100_METERS = "OUT_OF_100_METERS";

    private String code;

    private String msg;

    private String rootPath;

    private Payload payload;

    public ResponseDto() {
        this.code = FAIL_CODE;
    }

    public ResponseDto(String code) {
        super();
        this.code = code;
        this.msg = code;
    }


    public ResponseDto(String code, String msg) {
        super();
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Payload getPayload() {
        return payload;
    }

    public void setPayload(Payload payload) {
        this.payload = payload;
    }

    public String getRootPath() {
        return rootPath;
    }

    public void setRootPath(String rootPath) {
        this.rootPath = rootPath;
    }

    public boolean isCodeSucceed() {
        return code != null && SUCCESS_CODE.equals(code);
    }

    public static boolean isCodeSucceed(String code) {
        return TextUtils.equals(code, SUCCESS_CODE);
    }

    public boolean isOutOf100Meters() {
        return code != null && OUT_OF_100_METERS.equals(code);
    }

    public Object getPageData() {
        if (payload instanceof PageData) {
            return ((PageData) payload).getContent();
        } else return null;
    }

    @Override
    public String toString() {
        return "ResponseDto{" +
                "code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                ", rootPath='" + rootPath + '\'' +
                ", payload=" + payload +
                '}';
    }
}
